package cn.tedu.powerfulfitness.controller;

import cn.tedu.powerfulfitness.pojo.dto.ProductAddNewDTO;
import cn.tedu.powerfulfitness.pojo.dto.ProductUpdateDTO;
import cn.tedu.powerfulfitness.pojo.vo.ProductListItemVO;
import cn.tedu.powerfulfitness.pojo.vo.ProductStandardVO;
import cn.tedu.powerfulfitness.service.IProductService;
import cn.tedu.powerfulfitness.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/products")
@Api(tags = "05. 商品管理模块")
@Validated
public class ProductController {
    @Autowired
    private IProductService productService;

    @PostMapping("/add-new")
    @ApiOperation("添加商品")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid ProductAddNewDTO productAddNewDTO) {
        productService.addNew(productAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除商品")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "Integer")
    })
    public JsonResult delete(@Range(min = 1, message = "删除商品失败，ID值无效！")
                             @PathVariable Integer id) {
        productService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改商品详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "Integer")
    })
    public JsonResult updateInfoById(@Range(min = 1, message = "修改商品详情失败，ID值无效！")
                                     @PathVariable Integer id, @Valid ProductUpdateDTO productUpdateDTO) {
        productService.updateInfoById(id, productUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询商品详情")
    @ApiOperationSupport(order = 410)
    public JsonResult getStandardById(@Range(min = 1, message = "获取商品详情失败，ID值无效！")
                                      @PathVariable Integer id) {
        ProductStandardVO queryResult = productService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @ApiOperation("商品列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list() {
        List<ProductListItemVO> list = productService.list();
        return JsonResult.ok(list);
    }
}
